package com.business.system.action;

import com.business.system.bean.WxUserBean;
import com.business.system.util.DBTools;
import com.business.system.util.PageUtil;
import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.KeyGenerator;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.utils.WebUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.business.system.util.ResponseData;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: xudy
 * @date: 2018/04/10 15:08
 * @description:
 */
@RestController
@RequestMapping("user")
public class UserAction extends BaseAction {

    @RequestMapping("/login.do")
    public ResponseData loginUserAndPassword(Service service, HttpServletRequest request, @RequestParam String username, @RequestParam String password){
        WxUserBean bean = DBTools.getBean(service,WxUserBean.class,"select * from WX_USER where LOGIN_NAME=? AND PASSWORD=?",username,password);
        if(bean!=null){
            //设置token
            bean.setToken(KeyGenerator.getSystemUUID());
            DBUtils.update(service,bean);
            request.getSession().setAttribute("loginuser",bean);
            return ResponseData.buildResponseData(200,bean);
        }
        return null;
    }

    @RequestMapping("/logout.do")
    public ResponseData logOut(Service service, HttpServletRequest request){
        request.getSession().removeAttribute("loginuser");
        return ResponseData.buildResponseData(200,null);
    }
    @RequestMapping("/getUserInfo.do")
    public ResponseData getUserInfo(Service service,String token){
        WxUserBean bean = DBTools.getBean(service,WxUserBean.class,"select * from WX_USER where TOKEN=?",token);
        return ResponseData.buildResponseData(200,bean);
    }
    @RequestMapping("getUserList.do")
    public PageBean getUserList(Service service,HttpServletRequest request){
        PageBean page = PageUtil.page(request);
        StringBuilder sql = new StringBuilder();
        sql.append("select * from wx_user where 1=1");
        Map<String,Object> params = WebUtils.getRequestData(request);
        List<Object> condition = new ArrayList<>();
        if(params.containsKey("content")){
            sql.append(" and NICKNAME like '%"+params.get("content")+"%'");
        }
        DBTools.getPageList(service,WxUserBean.class,sql.toString(),page,condition);
        return page;
    }
}